﻿@typeparam TItem where TItem : VirtualServer

<MDialog @bind-Value="Value"
         MaxWidth="800"
         Persistent>
    <MCard>
        <MCardTitle>
            <span class="text-h5">虚拟服务器配置</span>
            <MSpacer />
            <MButton Icon
                     OnClick="CloseDialog">
                <MIcon>mdi-close</MIcon>
            </MButton>
        </MCardTitle>
        
        <MCardText>
            <MRow>
                <MCol Cols="12" Md="6">
                    <MTextField @bind-Value="Item.IpAddress"
                                Label="虚拟IP地址"
                                Outlined
                                Dense
                                Required
                                Rules="_requiredRules" />
                </MCol>
                <MCol Cols="12" Md="6">
                    <MTextField @bind-Value="Item.Port"
                    TValue="int"
                                Label="端口"
                                Type="number"
                                Outlined
                                Dense
                                Required
                                Rules="_requiredRules_int" />
                </MCol>
            </MRow>

            <MRow>
                <MCol Cols="12" Md="6">
                    <MSelect @bind-Value="Item.Protocol"
                             ItemText="item => item"
                             ItemValue="item => item"
                             Items="_protocolOptions"
                             Label="协议"
                             Outlined
                             Dense />
                </MCol>
                <MCol Cols="12" Md="6">
                    <MSelect @bind-Value="Item.LbAlgo"
                             ItemText="item => item"
                             ItemValue="item => item"
                             Items="_algorithmOptions"
                             Label="负载均衡算法"
                             Outlined
                             Dense />
                </MCol>
            </MRow>

            <MRow>
                <MCol Cols="12">
                    <MExpansionPanels Flat>
                        <MExpansionPanel>
                            <MExpansionPanelHeader Color="primary" HideActions>
                                <span class="text-h6">真实服务器</span>
                            </MExpansionPanelHeader>
                            <MExpansionPanelContent>
                                <RealServerList @bind-Items="Item.RealServers" OnAdd="()=>Item.RealServers.Add(new RealServer())" />
                            </MExpansionPanelContent>
                        </MExpansionPanel>
                    </MExpansionPanels>
                </MCol>
            </MRow>
        </MCardText>
        
        <MCardActions>
            <MSpacer />
            <MButton Color="primary"
                     OnClick="SaveItem">
                <MIcon Left>mdi-content-save</MIcon>
                保存
            </MButton>
            <MButton Color="secondary"
                     OnClick="CloseDialog">
                取消
            </MButton>
        </MCardActions>
    </MCard>
</MDialog>

@code {
    [Parameter]
    public bool Value { get; set; }

    [Parameter]
    public EventCallback<bool> ValueChanged { get; set; }

    [Parameter]
    public TItem Item { get; set; }

    [Parameter]
    public EventCallback<TItem> ItemChanged { get; set; }

    [Parameter]
    public EventCallback<TItem> OnSave { get; set; }

    private List<string> _protocolOptions = new() { "TCP", "UDP" };
    private List<string> _algorithmOptions = new() { "rr", "wrr", "lc", "wlc", "lblc", "sh", "dh" };
    private List<Func<string, StringBoolean>> _requiredRules = new()
    {
        value => string.IsNullOrWhiteSpace(value) ? "必填字段" : true
    };

    private List<Func<int, StringBoolean>> _requiredRules_int = new()
    {
        value => value==0 ? "必填字段" : true
    };

    private async Task SaveItem()
    {
        await OnSave.InvokeAsync(Item);
        await CloseDialog();
    }

    private async Task CloseDialog()
    {
        Value = false;
        await ValueChanged.InvokeAsync(Value);
    }
}